package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.commom.PageResult;
import com.qk.entity.User;
import com.qk.exception.BusinessExpection;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import vo.LoginResultVo;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> getUsers(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());

        List<User> users = userMapper.getUsers(userDto);
        Page<User> pageInfo = (Page<User>) users;
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());


    }
    @Override
    public void addUser(User user) {
        // 设置创建时间和更新时间为当前时间

        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        String password = user.getPassword()+ "123";
        user.setPassword(DigestUtils .md5DigestAsHex(password.getBytes()));

        userMapper.insert(user);
    }
    @Override
    public void deleteById(List<Integer> id) {
        userMapper.deleteById(id);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public void update(User user) {
        // 设置更新时间为当前时间
        user.setUpdateTime(LocalDateTime.now());
        // 密码加密处理
        user.setPassword(DigestUtils.md5DigestAsHex((user.getUsername()+"123").getBytes()));
        userMapper.update(user);

    }

    @Override
    public List<User> listByRole1(String roleLabel) {
        return userMapper.listByRole1(roleLabel);

    }

    @Override
    public LoginResultVo login(User user) {
        User u = userMapper.SelectByUsername(user.getUsername());
        if (u == null){

            throw new BusinessExpection("用户名或密码错误");
        }

        String _pass =DigestUtils.md5DigestAsHex((user.getPassword()).getBytes());
        if (!u.getPassword().equals(_pass)){
            throw new BusinessExpection("用户名或密码错误");

        }
        if (u.getStatus() != 1){

            throw new BusinessExpection("用户被停用");

        }
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());


        Map<String, Object>claims = new HashMap<>();
        claims.put("id", u.getId());
        claims.put("username", u.getUsername());
        String jwt = JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);
        return loginResultVo;

    }
    @Override
    public List<User> listAll() {
        return userMapper.listAll();
    }
    @Override
    public List<User> listByDept(Integer deptId) {
        return userMapper.listByDept(deptId);
    }
}













